package com.ocom.srvpaccess.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.dto.PerInfoDto;
import com.ocom.common.entity.human.EntityDeptInfo;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.vo.srvpaccess.UserDeptInfoVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.srvpaccess.dao.IDaoPersonnelInfo;
import com.ocom.srvpaccess.service.DeptInfoService;
import com.ocom.srvpaccess.service.PersonnelInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class PersonnelInfoServiceImpl extends ServiceImpl<IDaoPersonnelInfo, EntityPersonnelInfo> implements PersonnelInfoService {



    //personnelInfo 持久层
    @Autowired
    private IDaoPersonnelInfo personnelInfo;

    @Autowired
    private DeptInfoService deptInfoService;

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityPersonnelInfo> selectPerInfo(Long companyId, List<Long> pIds) {
//        QueryWrapper<EntityPersonnelInfo> personnelInfoQueryWrapper=Wrappers.query();
//        personnelInfoQueryWrapper.eq("com_id",companyId);
//        personnelInfoQueryWrapper.in("p_id",pIds);
//        return personnelInfo.selectList(personnelInfoQueryWrapper);
        System.out.println(companyId);
        System.out.println(pIds);
        log.info("白名单Id "+pIds);
        return personnelInfo.selectPerList(pIds);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public PerInfoDto selectPerInfo(Long companyId, Long pId) {
        return personnelInfo.selectPerInfo(pId);
    }
    // 4、根据idnum从人员信息表personnel_info查出userName、deptID；
    @Override
    public EntityPersonnelInfo getPerInfo(Long companyId, Long idnum) {
        QueryWrapper<EntityPersonnelInfo> query=Wrappers.query();
        query.eq("com_id",companyId);
        query.in("p_id",idnum);
        return personnelInfo.selectOne(query);
    }



    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public EntityPersonnelInfo regexpPerson(Long companyId, String name, String phone, String pIdentity) {
        QueryWrapper<EntityPersonnelInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", companyId);
        name=name.replace("*","_");
        phone=phone.replace("*","_");
        pIdentity=pIdentity.replace("*","_");
        queryWrapper.like("p_name",name);
        queryWrapper.like("p_phone",phone);
        queryWrapper.like("p_identity",pIdentity);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }


    /**
     *
     * @param companyId
     * @param card 身份证
     * @return
     */
    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public UserDeptInfoVo getPerByCard(Long companyId, String card) {
        UserDeptInfoVo userDeptInfoVo=new UserDeptInfoVo();
        QueryWrapper<EntityPersonnelInfo> personnelInfoQueryWrapper=Wrappers.query();
        personnelInfoQueryWrapper.eq("com_id",companyId);
        personnelInfoQueryWrapper.eq("p_identity",card);
        personnelInfoQueryWrapper.eq("status",0);
        personnelInfoQueryWrapper.last("limit 1");
        EntityPersonnelInfo entityPersonnelInfo=this.getOne(personnelInfoQueryWrapper);
        if(entityPersonnelInfo==null){
            return userDeptInfoVo;
        }
        userDeptInfoVo.setPId(entityPersonnelInfo.getPId().intValue());
        userDeptInfoVo.setPName(entityPersonnelInfo.getPName());
        userDeptInfoVo.setPCode(entityPersonnelInfo.getPCode());

        if(entityPersonnelInfo.getDeptId()==null  || entityPersonnelInfo.getDeptId()==0){
            return userDeptInfoVo;
        }
        EntityDeptInfo entityDeptInfo=deptInfoService.getDeptInfo(companyId,entityPersonnelInfo.getDeptId());
        if(entityDeptInfo==null){
            return userDeptInfoVo;
        }
        userDeptInfoVo.setDeptId(entityDeptInfo.getDeptId().intValue());
        userDeptInfoVo.setDeptName(entityDeptInfo.getDeptName());
        return userDeptInfoVo;
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityPersonnelInfo> selectGetPersonnelInfo(Long comId, Integer deviceNums) {
        QueryWrapper<EntityPersonnelInfo> personnelInfoQueryWrapper = Wrappers.query();
        personnelInfoQueryWrapper.eq("com_id",comId);
        personnelInfoQueryWrapper.eq("status",0);
        personnelInfoQueryWrapper.apply("char_length(p_identity)=18");
        personnelInfoQueryWrapper.orderByAsc("p_identity");
        personnelInfoQueryWrapper.last("limit "+deviceNums*2000);

        return personnelInfo.selectList(personnelInfoQueryWrapper);
    }
    
}
